﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSDBM.Code.dao;

public partial class Visual_CRUD_User_EditUser : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            
            try
            {
                String cod = Session["idselectuser"].ToString();
                VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
                if (usuariologado.acessgroup != 1)
                {
                    if (usuariologado.id != Convert.ToInt32(cod))
                    {
                        HttpContext.Current.Session["exception"] = "You are not authorized!";
                        Response.Redirect("~/Visual/Errors/error.aspx");
                    }
                }
                VSDBM.Code.dto.User objeto = UserDAO.DStoList(UserDAO.select(false, "WHERE id=" + cod, ""))[0];

                tbid.Value = objeto.id.ToString();
                tbname.Text = objeto.name;
                tbemail.Text = objeto.email;
                tblogin.Text = objeto.login;
                ddacessgroup.SelectedIndex = objeto.acessgroup;
                tbpassword.Value = objeto.password;
            }
            catch (Exception ex)
            {
                System.Console.Write(ex.Message);
            }
        }
    }

   
    
    protected void btyes_Click(object sender, ImageClickEventArgs e)
    {
        VSDBM.Code.dto.User usuario = new VSDBM.Code.dto.User(int.Parse(tbid.Value), tbname.Text, tbemail.Text, tblogin.Text, tbpassword.Value, ddacessgroup.SelectedIndex, 1);

        if (usuario.valida() == null)
        {
            if (UserDAO.edit(usuario))
            {
                Response.Redirect("ListUser.aspx");
            };
        }
        else
        {
            lbmensagem.Text = usuario.valida();
        }
    }

    protected void btno_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ListUser.aspx");
    }

    protected void btresetpass_Click(object sender, ImageClickEventArgs e)
    {
        VSDBM.Code.dto.User usuario = new VSDBM.Code.dto.User(int.Parse(tbid.Value), tbname.Text, tbemail.Text, tblogin.Text, tbpassword.Value, ddacessgroup.SelectedIndex, 1);

        if (usuario.valida() == null)
        {
            if (UserDAO.resetPassword(usuario))
            {
                lbmensagem.Text = "Senha resetada com sucesso. Essa senha agora é '123'.";
            }
            else
            {
                lbmensagem.Text = usuario.valida();
            }
        }


    }

    protected void btdelete_click(object sender, ImageClickEventArgs e)
    {
        VSDBM.Code.dto.User usuario = new VSDBM.Code.dto.User(int.Parse(tbid.Value), tbname.Text, tbemail.Text, tblogin.Text, tbpassword.Value, ddacessgroup.SelectedIndex, 1);

        if (usuario.valida() == null)
        {
            if (UserDAO.delete(usuario))
            {
                Response.Redirect("ListUser.aspx");
            }
            else
            {
                lbmensagem.Text = usuario.valida();
            }
        }
    }
}